import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login/LoginView.vue'),
    meta: {
      requiresAuth: false
    } // 不需要身份验证
  },
  {
    path: '/',
    component: () => import('../views/HomeView.vue'),
    redirect: '/start',
    meta: {
      requiresAuth: true
    },
    children: [
        {
          path: '/start',
          name: 'start',
          component: () => import('../views/start/StartView.vue'),
          meta: {
            requiresAuth: true
          }
        },
        {
          path: '/user',
          name: 'user',
          component: () => import('../views/user/UserView.vue'),
          meta: {
            requiresAuth: true
          }
        },
        {
          path: '/approval',
          name: 'approval',
          component: () => import('@/views/approval/ApprovalView.vue'),
          meta: {
            requiresAuth: true
          }
        },
        {
          path: '/message',
          name: 'message',
          component: () => import('../views/message/MessageView.vue'),
          meta: {
            requiresAuth: true
          }
        },
        {
          path: '/organization',
          name: 'organization',
          component: () => import('../views/organization/OrganizationView.vue'),
          meta: {
            requiresAuth: true
          }
        },
        {
          path: '/dict',
          name: 'dict',
          component: () => import('../views/dict/DictView.vue'),
          meta: {
            requiresAuth: true
          }
        },
        {
          path: '/post',
          name: 'post',
          component: () => import('../views/post/PostView.vue'),
          meta: {
            requiresAuth: true
          }
        },
        {
          path: '/role',
          name: 'role',
          component: () => import('../views/role/RoleView.vue'),
          meta: {
            requiresAuth: true
          }
        },
        {
          path: '/permission',
          name: 'permission',
          component: () => import('../views/permission/PermissionView.vue'),
          meta: {
            requiresAuth: true
          }
        },
        {
          path: '/rolePermissions',
          name: 'rolePermissions',
          component: () => import('../views/rolePermissions/RolePermissionsView.vue'),
          meta: {
            requiresAuth: true
          }
        },
        {
          path: '/assign',
          name: 'assign',
          component: () => import('../views/assign/AssignView.vue'),
          meta: {
            requiresAuth: true
          }
        },
        {
          path: '/arrangement',
          name: 'arrangement',
          component: () => import('../views/arrangement/ArrangementView.vue'),
          meta: {
            requiresAuth: true
          }
        },
        {
          path: '/documentOut',
          name: 'documentOut',
          component: () => import('../views/document/DocumentOutView.vue'),
          meta: {
            requiresAuth: true
          }
        },
        {
          path: '/documentSalary',
          name: 'documentSalary',
          component: () => import('../views/document/DocumentSalaryView.vue'),
          meta: {
            requiresAuth: true
          }
        },
        {
          path: '/attendanceAddress',
          name: 'attendanceAddress',
          component: () => import('../views/attendance/AttendanceAddressView.vue'),
          meta: {
            requiresAuth: true
          }
        },
        {
          path: '/attendanceSpecial',
          name: 'attendanceSpecial',
          component: () => import('../views/attendance/AttendanceSpecialView.vue'),
          meta: {
            requiresAuth: true
          }
        },
        {
          path: '/attendanceSys',
          name: 'attendanceSys',
          component: () => import('../views/attendance/AttendanceSysView.vue'),
          meta: {
            requiresAuth: true
          }
        },
        {
            path: '/attendanceAdmin',
            name: 'attendanceAdmin',
            component: () => import('../views/attendance/AttendanceAdminView.vue'),
            meta: {
              requiresAuth: true
            }
        },
        {
          path: '/attendanceCalendar',
          name: 'attendanceCalendar',
          component: () => import('../views/attendance/AttendanceCalendarView.vue'),
          meta: {
            requiresAuth: true
          }
        },
        {
          path: '/userOutList',
          name: 'userOutList',
          component: () => import('../views/user/UserOutView.vue'),
          meta: {
            requiresAuth: true
          }
        },
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to, from, next) => {
  const requiresAuth = to.meta.requiresAuth; // 获取路由配置中的 requiresAuth 字段

  // 判断用户是否已登录，存储在localStorage 中的登录状态
  const isLoggedIn = localStorage.getItem("token") != null;

  if (requiresAuth && !isLoggedIn) {
    // 如果请求路径需要身份验证且用户未登录，则重定向到登录页面
    next('/login');
  } else {
    // 否则，允许用户继续访问
    next();
  }
});

//解决Avoided redundant navigation to current location报错
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
};

export default router;
